package com.logistics.map.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.logistics.map.entity.domain.MapComponentsConvert;
import com.logistics.map.mapper.MapComponentsConvertMapper;
import com.logistics.utils.constants.Constants;
import com.logistics.utils.tool.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class MapComponentsConvertService extends ServiceImpl<MapComponentsConvertMapper, MapComponentsConvert> {


    /**
     * 查找平衡重点位
     * @param location
     * @return
     */
    public Map<String, String> balanceStationByLocation(List<String> location) {
         List<MapComponentsConvert> result =  this.list(new LambdaQueryWrapper<MapComponentsConvert>().
                in(MapComponentsConvert::getLocation, location));
         return this.list(new LambdaQueryWrapper<MapComponentsConvert>().in(MapComponentsConvert::getLabel,
                         result.stream().map(MapComponentsConvert::getLabel).collect(Collectors.toList()))).stream().
                filter(e -> StringUtils.isEquals(e.getAgvType(), Constants.AGV_TYPE_BALANCE))
                .collect(Collectors.toMap(MapComponentsConvert::getLabel,
                        MapComponentsConvert::getLocation));
    }

    /**
     * 查找叉车点位
     * @param location
     * @return
     */
    public Map<String, String> forkliftStationByLocation(List<String> location) {
        List<MapComponentsConvert> result = this.list(new LambdaQueryWrapper<MapComponentsConvert>().
                in(MapComponentsConvert::getLocation, location));
        return this.list(new LambdaQueryWrapper<MapComponentsConvert>().in(MapComponentsConvert::getLabel,
                        result.stream().map(MapComponentsConvert::getLabel).collect(Collectors.toList()))).stream().
                filter(e -> StringUtils.isEquals(e.getAgvType(), Constants.AGV_TYPE_FORKLIFT))
                .collect(Collectors.toMap(MapComponentsConvert::getLabel,
                        MapComponentsConvert::getLocation));
    }

    /**
     * 查找叉车站点位置
     * @param location
     * @return
     */
    public String forkliftStationByLocation(String location) {
        MapComponentsConvert result = this.getOne(new LambdaQueryWrapper<MapComponentsConvert>().
                in(MapComponentsConvert::getLocation, location));
        MapComponentsConvert resultInfo = this.getOne(new LambdaQueryWrapper<MapComponentsConvert>()
                .eq(MapComponentsConvert::getLabel, result.getLabel()).
                eq(MapComponentsConvert::getAgvType, Constants.AGV_TYPE_FORKLIFT));
        return StringUtils.isNotNull(resultInfo) ? resultInfo.getLocation() : null;
    }
}
